package tp.pr5.instructions;

import tp.pr5.NavigationModule;
import tp.pr5.RobotEngine;
import tp.pr5.instructions.exceptions.InstructionExecutionException;
import tp.pr5.instructions.exceptions.WrongInstructionFormatException;
import tp.pr5.items.*;

public class ScanInstruction extends UndoAble{
	String objeto;
	RobotEngine rbt;
	private ItemContainer rbtCont;
	public ScanInstruction(){
		this.objeto = null;
	}
	public ScanInstruction(String object){
		this.objeto = object;
	}
	
	@Override
	public Instruction parse(String cad) throws WrongInstructionFormatException {
		String[] lineArr = cad.trim().replaceAll(" +", " ").split(" ");	//parte la cadena introducida en palabras
		
		String accion = "";
		String id = "";
		if(lineArr.length>=1) accion = lineArr[0]; //si existe alguna palabra, la primera serÃ¡ la acciÃ³n
		if(lineArr.length==2) id = lineArr[1]; //si hay mÃ¡s de una palabra, la segunda serÃ¡ la rotaciÃ³n		
		
		if(accion.equalsIgnoreCase("SCAN") || accion.equalsIgnoreCase("ESCANEAR")){//si la acciÃ³n introducida es SCAN
			if(lineArr.length == 1){
				return new ScanInstruction();
			}else if (lineArr.length == 2){
				return new ScanInstruction(id);
			}else{
				throw new WrongInstructionFormatException();
			}
		}else{
			throw new WrongInstructionFormatException();
		}
	}

	@Override
	public String getHelp() {
		return "     SCAN|ESCANEAR [ <ITEM> ]";
	}

	@Override
	public void configureContext(RobotEngine engine,
			NavigationModule navigation, ItemContainer robotContainer) {
		this.rbt = engine;
		this.rbtCont = robotContainer;
	}

	@Override
	public void execute() throws InstructionExecutionException {
		String insId = this.objeto;
		if(this.rbtCont.containsItem(insId))
			this.rbtCont.requestScanItem(insId);
		else throw new InstructionExecutionException("The container does not contain the item.");
		
	}
	@Override
	public boolean isUndoAble() {

		return false;
	}
	/**
	 * revets the las instruction
	 */
	
}
